package com.zlc.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlc.gulimall.product.dao.AttrDao;
import com.zlc.gulimall.product.dao.CategoryDao;
import com.zlc.gulimall.product.entity.*;
import com.zlc.gulimall.product.service.AttrService;
import com.zlc.gulimall.product.vo.AttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlc.common.utils.PageUtils;
import com.zlc.common.utils.Query;

import com.zlc.gulimall.product.dao.AttrGroupDao;
import com.zlc.gulimall.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    AttrDao attrDao;

    @Resource
    AttrGroupDao attrGroupDao;

    @Resource
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCatelogId(Map<String, Object> params, Long catelogId) {
        //如果catelogId = 0 则 默认查询所有
        IPage<AttrGroupEntity> page = null;
        if(catelogId == 0){
            page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    //模糊查询  好像是?
                    new LambdaQueryWrapper<>()
            );
            return new PageUtils(page);
        }
        //如果有catelogId
        page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new LambdaQueryWrapper<AttrGroupEntity>()
                        .eq(AttrGroupEntity::getCatelogId,catelogId)
        );

        return new PageUtils(page);
    }

    @Override
    public List<AttrGroupVo> queryByCatelogId(Long catelogId) {

        //1.查询属性分组信息
        List<AttrGroupEntity> attrGroups = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>()
                .eq(AttrGroupEntity::getCatelogId, catelogId));

        //2.查询该分组下面所有属性信息
        List<AttrGroupVo> attrGroupVos = attrGroups.stream().map((item) -> {
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(item, attrGroupVo);
            List<AttrEntity> attrs = attrService.getRelationAttrs(attrGroupVo.getAttrGroupId());
            attrGroupVo.setAttrs(attrs);
            //返回
            return attrGroupVo;
        }).collect(Collectors.toList());


        return attrGroupVos;
    }

}